#!/usr/bin/python

import ApiDefinition
import ByteArraySerializer

class IpMgrDefinition(ApiDefinition.ApiDefinition):
    '''
    \ingroup ApiDefinition
    \brief API definition for the IP manager.
   
    \note This class inherits from ApiDefinition. It redefines the attributes of
          its parents class, but inherits the methods.
    '''
    
    STRING    = ApiDefinition.FieldFormats.STRING
    BOOL      = ApiDefinition.FieldFormats.BOOL
    INT       = ApiDefinition.FieldFormats.INT
    INTS      = ApiDefinition.FieldFormats.INTS
    HEXDATA   = ApiDefinition.FieldFormats.HEXDATA
    RC        = ApiDefinition.ApiDefinition.RC
    SUBID1    = ApiDefinition.ApiDefinition.SUBID1
    SUBID2    = ApiDefinition.ApiDefinition.SUBID2
    RC_OK     = ApiDefinition.ApiDefinition.RC_OK
    
    def __init__(self):
        self.serializer = ByteArraySerializer.ByteArraySerializer(self)
    
    def default_serializer(self,commandArray,fieldsToFill):
        '''
        \brief IpMgrDefinition-specific implementation of default serializer
       
        \param commandArray   An array of the form [commandName, subCommandname]
                              The array can be of any length, and is of length 1
                              if no subcommands are used.
        \param fieldsToFill   The list of fields to send, organized as a
                              dictionary of the form
                              <tt>fieldname:fieldvalue</tt>.
       
        \returns id,byteString where id is the command ID and byteArray an array
                               of bytes
        '''
        return self.serializer.serialize(commandArray,fieldsToFill)
    
    def deserialize(self,type,cmdId,byteArray):
        '''
        \brief IpMgrDefinition-specific implementation of deserializer
        '''
        return self.serializer.deserialize(type,cmdId,byteArray)
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    fieldOptions = {
        RC : [
            [ 0,   'OK',                    ''],
            [ 1,   'inv_cmd',               'invalid command'],
            [ 2,   'inv_arg',               'invalid argument'],
            [ 3,   'INVALID_AUTHENTICATION','Invalid Authentication (MUX)'],
            [ 4,   'INVALID_API_VERSION',   'Invalid API version (MUX)'],
            [ 5,   'comd_timeout',          'command timeout (MUX)'],
            [11,   'e_list',                'end of list'],
            [12,   'no_resources',          'reached maximum number of items'],
            [13,   'in_progress',           'operation is in progress'],
            [14,   'NACK',                  'negative acknowledge'],
            [15,   'write_error',           'flash write failed'],
            [16,   'validation_error',      'parameter validation error'],
        ],
        'successCode' : [
            [ 0,   'success',               ''],
            [ 1,   'unsupported_version',   ''],
            [ 2,   'invalid_mode',          ''],
        ],
        'mode' : [
            [ 0,   'legacy',                ''],
        ],        
        # 'notifType' : [
        #     [ 1,   'event',                 'event notification'],
        #     [ 2,   'log',                   'log message'],
        #     [ 4,   'data',                  'data payload'],
        #     [ 5,   'ipdata',                '6LoWPAN packet'],
        #     [ 6,   'hr',                    'health report'],
        # ],
        # 'eventType' : [
        #     [ 0,   'mote_reset',            'eventData is mac address'],
        #     [ 1,   'net_reset',             'eventData is empty'],
        #     [ 2,   'cmd_finish ',           'eventData is struct cmdFinish'],
        #     [ 3,   'mote_join',             'eventData is mac address'],
        #     [ 4,   'mote_oper',             'eventData is mac address'],
        #     [ 5,   'mote_lost',             'eventData is mac address'],
        #     [ 6,   'net_time',              'eventData is struct spl_timeEvent'],
        #     [ 7,   'ping_resp',             'eventData is struct spl_pingResp'],
        #     [ 9,   'mote_bandwidth',        'eventData is mac address'],
        #     [10,   'path_create',           'eventData is struct path'],
        #     [11,   'path_delete',           'eventData is struct path'],
        #     [12,   'packet_sent',           'eventData is struct cmdFinish'],
        #     [13,   'mote_create',           'eventData is struct moteinfo'],
        #     [14,   'mote_delete',           'eventData is struct moteinfo'],
        # ],
        # 'yesNo' : [
        #     [ 1,   'yes',                   ''],
        #     [ 0,   'no',                    ''],
        # ],
        'frameProfile' : [
            [ 1,   'picard_profile_01',     'fast  build / medium operation'],
        ],
        'advState' : [
            [ 0,   'ON',                    ''],
            [ 1,   'OFF',                   ''],
        ],
        'priority': [
            [0,    'LOW',                   'low'],
            [1,    'MED',                   'medium'],
            [2,    'HIGH',                  'high'],
            [3,    'CTRL',                  'control'],
        ],
        'downstreamMode' : [
            [ 0,   'normal',                ''],
            [ 1,   'fast',                  ''],
        ],
        'locMode' : [
            [ 0,   'OFF',                   ''],
            [ 1,   'ondemand',              ''],
            [ 2,   'rtls',                  ''],
        ],
        'netState' : [
            [ 0,   'oper',                  'Network is operating normally'],
            [ 1,   'radiotest',             'Manager is in radiotest mode'],
            [ 2,   'notstarted',            'Waiting for "startNetwork" API command'],
            [ 3,   'err_unexp',             'Unexpected error occurred at startup'],
            [ 4,   'err_config',            'Invalid or not licensed configuration found at startup'],
            [ 5,   'err_license',           'Invalid license file found at startup'],
        ],
        'moteState' : [
            [ 0,   'lost',                  'mote is not currently part of the network'],
            [ 1,   'negot',                 'mote is in the process of joining the  network'],
            [ 4,   'oper',                  'mote is operational'],
        ],
        'userRole' : [
            [ 0,   'viewer',                ''],
            [ 1,   'admin',                 ''],
        ],
        'resetType' : [
            [ 0,   'system',                ''],
            [ 2,   'mote',                  ''],
        ],
        'bbMode' : [
            [ 0,   'OFF',                   ''],
            [ 1,   'up',                    ''],
            [ 2,   'bidir',                 ''],
        ],
        'pathFilter' : [
            [ 0,   'all',                   ''],
            [ 1,   'upstream',              ''],
        ],
        'pathDirection' : [
            [ 0,   'NA',                    'No path'],
            [ 1,   'notused',               'Path is not used'],
            [ 2,   'up',                    'Upstream path'],
            [ 3,   'down',                  'Downstream path'],
        ]
    }
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    commands = [
        # command 'hello' (commandID 1) is handled by the Hdlc module
        # command 'hello_response' (commandID 2) is handled by the Hdlc module
        {
            'id'         :  1,
            'name'       :  'mux_hello',
            'description':  'Sent by the manager to initiate a new session with a client.',
            'request'    : [
                ['version',                 INT,      1,   None],
                ['secret',                  HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['version',             INT,      1,   None],
                ],
            },
        },
        {
            'id'         :  1,
            'name'       :  'hello',
            'description':  '',
            'request'    : [
                ['version',                 INT,      1,   None],
                ['cliSeqNo',                INT,      1,   None],
                ['mode',                    INT,      1,   True],
            ],
        },
        {
            'id'         : 2,
            'name'       : 'hello_response',
            'description': '',
            'request'    : [], # there is no request
            'response'   : {
                'FIELDS':  [
                    ['successCode',         INT,      1,   True],
                    ['version',             INT,      1,   None],
                    ['mgrSeqNo',            INT,      1,   None],
                    ['cliSeqNo',            INT,      1,   None],
                    ['mode',                INT,      1,   True],
                ],
            },
        },
        
        {
            'id'         : 21,
            'name'       : 'reset',
            'description': 'Clear or restart various objects. The command argument is an object type, and if the object is a mote the \'mac\' must be specified (otherwise that argument is ignored).',
            'request'    : [
                ['resetType',               INT,      1,   True],
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 22,
            'name'       : 'subscribe',
            'description': 'The notification filter is a bitmask of flags indicating the types of notifications that the client wants to receive.\nAll notifications are sent using the reliable protocol, so subsequent notification packets will be queued while waiting for an acknowledgment.\nEach subscription request overwrites the previous one. If a client is subscribed to data and then decides he also wants events he should send a subscribe command with both the data and event flags set. To clear all subscriptions, the client should send a subscribe command with the filter set to zero.\nWhen a session is initiated between the manager and a client, the subscription state is initialized to inactive.\n\nSubscription codes are:\n- 0x02: events\n- 0x04: log\n- 0x10: data\n- 0x20: IP data\n- 0x40: hr\nFor example, to receive all notifications, enter 0x02 | 0x04 | 0x10 | 0x20 | 0x40 = 0x76 = 118',
            'request' : [
                ['filter',                  INT,      4,   None],
                ['unreliableFilter',        INT,      4,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 23,
            'name'       : 'getTime',
            'description': 'Return the current UTC time and the current Absolute Slot Number.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['uptime',              INT,      4,   None],
                    ['utcSecs',             INT,      8,   None],
                    ['utcUsecs',            INT,      4,   None],
                    ['asn',                 HEXDATA,  5,   None],
                    ['asnOffset',           INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 26,
            'name'       : 'setNetworkCfg',
            'description': 'Change network configuration parameters. The response code indicates whether the change was successful.',
            'request'    :  [
                ['networkId',               INT,      2,   None],
                ['apTxPower',               INT,      1,   None],
                ['frameProfile',            INT,      1,   True],
                ['maxMotes',                INT,      2,   None],
                ['baseBandwidth',           INT,      2,   None],
                ['downFrameMultVal',        INT,      1,   None],
                ['numParents',              INT,      1,   None],
                ['enableCCA',               INT,      1,   None],
                ['channelList',             INT,      2,   None],
                ['autoStartNetwork',        INT,      1,   None],
                ['locMode',                 INT,      1,   True],
                ['bbMode',                  INT,      1,   True],
                ['bbSize',                  INT,      1,   None],
                ['isRadioTest',             INT,      1,   None],
                ['bwMult',                  INT,      2,   None],
                ['oneChannel',              INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 31,
            'name'       : 'clearStatistics',
            'description': 'Clear the accumulated network statistics. Does not clear the path quality or mote statistics.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 33,
            'name'       : 'exchangeMoteJoinKey',
            'description': 'Instruct the manager to send a new join key to the mote specified as a parameters. The response contains a callbackId. A \'Command Finished\' notification with the callbackId is sent when the operation completes.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['key',                     HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 34,
            'name'       : 'exchangeNetworkId',
            'description': 'Instruct the manager to distribute a new network id to the motes. A \'Command Finished\' notification with the resulting callback id is sent when operation completes.',
            'request'    : [
                ['id',                      INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 35,
            'name'       : 'radioTestTx',
            'description': 'Switch the manager to a special "radioTest" mode (if it\'s not already there) and executes the radio transmit test.',
            'request'    : [
                ['type',                    INT,      1,   None],
                ['mask',                    INT,      2,   None],
                ['numRepeats',              INT,      2,   None],
                ['txPower',                 INTS,     1,   None],
                ['numPackets',              INT,      1,   None],
                ['pkSize1',                 INT,      1,   None],
                ['gap1',                    INT,      2,   None],
                ['pkSize2',                 INT,      1,   None],
                ['gap2',                    INT,      2,   None],
                ['pkSize3',                 INT,      1,   None],
                ['gap3',                    INT,      2,   None],
                ['pkSize4',                 INT,      1,   None],
                ['gap4',                    INT,      2,   None],
                ['pkSize5',                 INT,      1,   None],
                ['gap5',                    INT,      2,   None],
                ['pkSize6',                 INT,      1,   None],
                ['gap6',                    INT,      2,   None],
                ['pkSize7',                 INT,      1,   None],
                ['gap7',                    INT,      2,   None],
                ['pkSize8',                 INT,      1,   None],
                ['gap8',                    INT,      2,   None],
                ['pkSize9',                 INT,      1,   None],
                ['gap9',                    INT,      2,   None],
                ['pkSize10',                INT,      1,   None],
                ['gap10',                   INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 37,
            'name'       : 'radioTestRx',
            'description': 'Switch the manager to a special "radioTest" mode (if it\'s not already there) and executes the radio receive test.',
            'request'    : [
                ['mask',                    INT,      2,   None],
                ['duration',                INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 38,
            'name'       : 'radioTestStats',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['rxOk',                INT,      2,   None],
                    ['rxFail',              INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 39,
            'name'       : 'setAclEntry',
            'description': '',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['joinKey',                 HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 40,
            'name'       : 'getNextAclEntry',
            'description': 'Returns information about the next mote\' entry in the Access Control List. To begin a search (i.e. find the first mote), use a zero MAC address (00:00:00:00:00:00:00:00).\nThere is no mechanism for reading the ACL entry of a specific mote. This call is an iterator. If you call this funcation with mote A as the argument, your response is the ACL entry for mote B, where B is the next mote in the ACL.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                    ['joinKey',             HEXDATA,  16,  None],
                ],
            },
        },
        {
            'id'         : 41,
            'name'       : 'delAclEntry',
            'description': '',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 42,
            'name'       : 'pingMote',
            'description': 'Ping the specified mote. A request is sent to that mote. A unique callbackId is generated and returned with the response. When a reply is received from the mote, the manager generates a notification with the same callbackId and the measured round trip delay.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 43,
            'name'       : 'getLog',
            'description': 'Retrieve logs from the manager or a mote specified by MAC address.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 44,
            'name'       : 'sendData',
            'description': 'Send a packet to a mote in the network, and return a callbackId. When the packet is injected into the network, the manager issues a \'packet sent\' notification which contains the callbackId. It is the application layer\'s responsibility to timeout if no response is received.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['priority',                INT,      1,   True],
                ['srcPort',                 INT,      2,   None],
                ['dstPort',                 INT,      2,   None],
                ['options',                 INT,      1,   None],
                ['data',                    HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 45,
            'name'       : 'startNetwork',
            'description': 'Instruct the manager to start forming the network, i.e. to begin accepting join requests from devices.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 46,
            'name'       : 'getSysInfo',
            'description': 'Return various system information.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                    ['hwModel',             INT,      1,   None],
                    ['hwRev',               INT,      1,   None],
                    ['swMajor',             INT,      1,   None],
                    ['swMinor',             INT,      1,   None],
                    ['swPatch',             INT,      1,   None],
                    ['swBuild',             INT,      2,   None],
                ],
            },

        },
        {
            'id'         : 47,
            'name'       : 'getMoteCfg',
            'description': 'Return a single mote description. The command takes two arguments, a MAC Address and a flag indicating whether the MAC Address refers to the requested mote or to the next mote in manager\'s memory.\nThis command may be used to iterate through all motes known by the manager by starting with the mac parameter set to 00:00:00:00:00:00:00:00 and next set to true, and then using the MAC Address of that response as the input to the next call.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['next',                    INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                    ['moteId',              INT,      2,   None],
                    ['isAP',                INT,      1,   None],
                    ['moteState',           INT,      1,   True],
                    ['mobilityType',        INT,      1,   None],
                    ['isRouting',           INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 48,
            'name'       : 'getPathInfo',
            'description': 'Return parameters for the requested path.',
            'request'    : [
                ['source',                  HEXDATA,  8,   None],
                ['dest',                    HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['source',              HEXDATA,  8,   None],
                    ['dest',                HEXDATA,  8,   None],
                    ['pathDirection',       INT,      1,   True],
                    ['numLinks',            INT,      1,   None],
                    ['quality',             INT,      1,   None],
                    ['rssiSrcDest',         INTS,     1,   None],
                    ['rssiDestSrc',         INTS,     1,   None],
                ],
            },
        },
        {
            'id'         : 49,
            'name'       : 'getNextPath',
            'description': 'Iterate across paths connected to a particular mote. The pathId parameter indicates the previous value in the iteration. pathId 0 returns the first path.\nA pathId can not be used as a unique identifier for a path. It is only valid when associated with a particular mote.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['pathFilter',              INT,      1,   True],
                ['pathId',                  INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['pathId',              INT,      2,   None],
                    ['source',              HEXDATA,  8,   None],
                    ['destination',         HEXDATA,  8,   None],
                    ['pathDirection',       INT,      1,   True],
                    ['numLinks',            INT,      1,   None],
                    ['quality',             INT,      1,   None],
                    ['rssiSrcDest',         INTS,     1,   None],
                    ['rssiDestSrc',         INTS,     1,   None],
                ],
            },
        },
        {
            'id'         : 50,
            'name'       : 'setAdvertising',
            'description': 'Instruct the manager to activate or deactivate advertising. The response is a callbackId. A \'Command Finished\' notification with the same callbackId is generated when the command completes.',
            'request'    : [
                ['activate',                INT,      1,   'advState'],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 51,
            'name'       : 'setDownsteamFrameMode',
            'description': 'Instruct the manager to shorten or extend the downstream frame. Once this command is executed, the manager switches to manual mode and no longer changes frame size automatically. The response is a callbackId. A \'Command Finished\' notification with the same callbackId is generated when the command completes.',
            'request'    : [
                ['frameMode',               INT,      1,   'downstreamMode'],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 52,
            'name'       : 'measureRSSI',
            'description': 'Start a distributed RSSI measurement across all channels. Motes send results via data notifications directly to the application.\nNote: this feature is NOT implemented in the SmartMesh IP 1.0.1 firmware.',
            'request'    : [
                ['duration',                INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 53,
            'name'       : 'getManagerStatistics',
            'description': 'Return dynamic information and statistics about the manager. Staistics-related counters are cleared together with all current statistics when calling clearStatistics.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['serTxCnt',            INT,      2,   None],
                    ['serRxCnt',            INT,      2,   None],
                    ['serRxCRCErr',         INT,      2,   None],
                    ['serRxOverruns',       INT,      2,   None],
                    ['apiEstabConn',        INT,      2,   None],
                    ['apiDropppedConn',     INT,      2,   None],
                    ['apiTxOk',             INT,      2,   None],
                    ['apiTxErr',            INT,      2,   None],
                    ['apiTxFail',           INT,      2,   None],
                    ['apiRxOk',             INT,      2,   None],
                    ['apiRxProtErr',        INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 54,
            'name'       : 'setTime',
            'description': 'Set the UTC time on the manager. This command may only be executed when the network is not running.\nIf the timePin flag is false, the manager sets the specified time as soon as it receives the setTime command.\nWhen the manager receives a timePin trigger, it temporarily stores the current time. If a setTime request is received within a short period of time following the timePin trigger, the manager calculates the delay since the timePin trigger and sets the time such that the timePin trigger was received at the specified time value.',
            'request'    : [
                ['timePin',                 INT,      1,   None],
                ['utcSecs',                 INT,      8,   None],
                ['utcUsecs',                INT,      4,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 55,
            'name'       : 'getLicense',
            'description': 'Return the current license key',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['licenseString',       HEXDATA,  13,  None],
                ],
            },
        },
        {
            'id'         : 56,
            'name'       : 'setLicense',
            'description': '',
            'request'    : [
                ['licenseString',           HEXDATA,  13,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 58,
            'name'       : 'setUser',
            'description': 'Set the password that must be used to log into the command line for a particular user role.',
            'request'    : [
                ['userRole',                INT,      1,   True],
                ['password',                HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 59,
            'name'       : 'sendIP',
            'description': 'Send a 6LoWPAN packet to a mote in the network. The returned callbackId is included in the \'packet sent\' notification when this packet is injected into the network. The application is responsible for constructing a valid 6LoWPAN packet.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['priority',                INT,      1,   True],
                ['options',                 INT,      1,   None],
                ['encryptedOffset',         INT,      1,   None],
                ['data',                    HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 60,
            'name'       : 'startLocation',
            'description': 'Send a request to a set of motes to generate distance measurements to a mobile mote. The manager sends a series of commands to the specified motes and activates the location links for enough time to perform the requested measurements.\nMore than one startLocation request may be queued up by the manager, but the queue size is limited. The caller must be able to handle a \'queue full\' error and retry.\nThe startLocation request contains a variable number of fixed motes. The manager determines the number of fixed motes provided by the caller by checking the length of the request. The length of the fixed motes array must be between 1 and 10.\nThe startLocation call returns a callbackId that is used to inform the caller of the progress of the distance measurement. Location events are similar to \'Command Finished\' events\nThe distance measurements are returned as \'Data\' notifications from the fixed motes.',
            'request'    : [
                ['numFrames',               INT,      1,   None],
                ['mobileMote',              HEXDATA,  8,   None],
                ['fixedMotes',              HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['callbackId',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 61,
            'name'       : 'restoreFactoryDefaults',
            'description': 'Remove the following files from the manager, thereby switching to the default settings: config, acl, ini.\nThis command does affect the license file.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 62,
            'name'       : 'getMoteInfo',
            'description': 'Return dynamic information about the specified mote.',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                    ['moteState',           INT,      1,   True],
                    ['numNbrs',             INT,      1,   None],
                    ['numGoodNbrs',         INT,      1,   None],
                    ['requestedBw',         INT,      4,   None],
                    ['totalNeededBw',       INT,      4,   None],
                    ['assignedBw',          INT,      4,   None],
                    ['packetsReceived',     INT,      4,   None],
                    ['packetsLost',         INT,      4,   None],
                    ['avgLatency',          INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 63,
            'name'       : 'getNetworkCfg',
            'description': 'Return general network configuration parameters, including the network id, bandwidth parameters, and number of motes.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['networkId',           INT,      2,   None],
                    ['apTxPower',           INT,      1,   None],
                    ['frameProfile',        INT,      1,   True],
                    ['maxMotes',            INT,      2,   None],
                    ['baseBandwidth',       INT,      2,   None],
                    ['downFrameMultVal',    INT,      1,   None],
                    ['numParents',          INT,      1,   None],
                    ['enableCCA',           INT,      1,   None],
                    ['channelList',         INT,      2,   None],
                    ['autoStartNetwork',    INT,      1,   None],
                    ['locMode',             INT,      1,   True],
                    ['bbMode',              INT,      1,   True],
                    ['bbSize',              INT,      1,   None],
                    ['isRadioTest',         INT,      1,   None],
                    ['bwMult',              INT,      2,   None],
                    ['oneChannel',          INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 64,
            'name'       : 'getNetworkInfo',
            'description': 'Return dynamic network information and statistics.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['numMotes',            INT,      2,   None],
                    ['asnSize',             INT,      2,   None],
                    ['advertisementState',  INT,      1,   'advState'],
                    ['downstreamMode',      INT,      1,   True],
                    ['netReliability',      INT,      1,   None],
                    ['netPathStability',    INT,      1,   None],
                    ['netLatency',          INT,      4,   None],
                    ['netState',            INT,      1,   True],
                    ['ip6addr',             HEXDATA,  16,  None],
                ],
            },
        },
        {
            'id'         : 65,
            'name'       : 'getMoteCfgById',
            'description': 'Return the description about a single mote as the response, identified by its Short Address (mote id). The structure of the response is the same as the structure of the response of the getMoteCfg command.',
            'request'    : [
                    ['moteId',              INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['mac',                 HEXDATA,  8,   None],
                    ['moteId',              INT,      2,   None],
                    ['isAP',                INT,      1,   None],
                    ['state',               INT,      1,   None],
                    ['mobilityType',        INT,      1,   None],
                    ['isRouting',           INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 66,
            'name'       : 'setCommonJoinKey',
            'description': '',
            'request'    : [
                ['key',                     HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 67,
            'name'       : 'getIPCfg',
            'description': 'Return IP configuration parameters, including the IPv6 address and mask.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['ip6addr',             HEXDATA,  16,  None],
                    ['mask',                HEXDATA,  16,  None],
                ],
            },
        },
        {
            'id'         : 68,
            'name'       : 'setIPCfg',
            'description': '',
            'request'    : [
                ['ip6addr',                 HEXDATA,  16,  None],
                ['mask',                    HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 69,
            'name'       : 'delMote',
            'description': '',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 70,
            'name'       : 'getMoteLinks',
            'description': '',
            'request'    : [
                ['mac',                     HEXDATA,  8,   None],
                ['idx',                     INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['idx',                 INT,      2,   None],
                    ['u',                   INT,      1,   None],
                    ['numLinks',            INT,      1,   None],
                    ['frameId1',            INT,      1,   None],  # 1
                    ['slot1',               INT,      4,   None],
                    ['channel1',            INT,      1,   None],
                    ['moteId1',             INT,      2,   None],
                    ['flags1',              INT,      1,   None],
                    ['frameId2',            INT,      1,   None],  # 2
                    ['slot2',               INT,      4,   None],
                    ['channel2',            INT,      1,   None],
                    ['moteId2',             INT,      2,   None],
                    ['flags2',              INT,      1,   None],
                    ['frameId3',            INT,      1,   None],  # 3
                    ['slot3',               INT,      4,   None],
                    ['channel3',            INT,      1,   None],
                    ['moteId3',             INT,      2,   None],
                    ['flags3',              INT,      1,   None],
                    ['frameId4',            INT,      1,   None],  # 4
                    ['slot4',               INT,      4,   None],
                    ['channel4',            INT,      1,   None],
                    ['moteId4',             INT,      2,   None],
                    ['flags4',              INT,      1,   None],
                    ['frameId5',            INT,      1,   None],  # 5
                    ['slot5',               INT,      4,   None],
                    ['channel5',            INT,      1,   None],
                    ['moteId5',             INT,      2,   None],
                    ['flags5',              INT,      1,   None],
                    ['frameId6',            INT,      1,   None],  # 6
                    ['slot6',               INT,      4,   None],
                    ['channel6',            INT,      1,   None],
                    ['moteId6',             INT,      2,   None],
                    ['flags6',              INT,      1,   None],
                    ['frameId7',            INT,      1,   None],  # 7
                    ['slot7',               INT,      4,   None],
                    ['channel7',            INT,      1,   None],
                    ['moteId7',             INT,      2,   None],
                    ['flags7',              INT,      1,   None],
                    ['frameId8',            INT,      1,   None],  # 8
                    ['slot8',               INT,      4,   None],
                    ['channel8',            INT,      1,   None],
                    ['moteId8',             INT,      2,   None],
                    ['flags8',              INT,      1,   None],
                    ['frameId9',            INT,      1,   None],  # 9
                    ['slot9',               INT,      4,   None],
                    ['channel9',            INT,      1,   None],
                    ['moteId9',             INT,      2,   None],
                    ['flags9',              INT,      1,   None],
                    ['frameId10',           INT,      1,   None],  # 10
                    ['slot10',              INT,      4,   None],
                    ['channel10',           INT,      1,   None],
                    ['moteId10',            INT,      2,   None],
                    ['flags10',             INT,      1,   None],
                ],
            },
        },
    ]
    
    subCommandsEvents = [
        {
            'id'         : 0,
            'name'       : 'eventMoteReset',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 1,
            'name'       : 'eventNetReset',
            'description': '',
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 2,
            'name'       : 'eventCmdFinish',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['callbackId',         INT,      4,   None],
                    ['result',             INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 3,
            'name'       : 'eventMoteJoin',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 4,
            'name'       : 'eventMoteOper',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 5,
            'name'       : 'eventMoteLost',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 6,
            'name'       : 'eventNetTime',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['uptime',             INT,      4,   None],
                    ['utcSecs',            INT,      8,   None],
                    ['utcUsecs',           INT,      4,   None],
                    ['asn',                HEXDATA,  5,   None],
                    ['asnOffset',          INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 7,
            'name'       : 'eventPing',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['callbackId',         INT,      4,   None],
                    ['mac',                HEXDATA,  8,   None],
                    ['delay',              INT,      4,   None],
                    ['voltage',            INT,      2,   None],
                    ['temperature',        INTS,      1,   None],
                ],
            },
        },
        {
            'id'         : 9,
            'name'       : 'eventBandwidth',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 10,
            'name'       : 'eventPathCreate',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['source',             HEXDATA,  8,   None],
                    ['destination',        HEXDATA,  8,   None],
                    ['pathDirection',      INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 11,
            'name'       : 'eventPathDelete',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['source',             HEXDATA,  8,   None],
                    ['destination',        HEXDATA,  8,   None],
                    ['pathDirection',      INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 12,
            'name'       : 'eventPacketSent',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['callbackId',         INT,      4,   None],
                    ['result',             INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 13,
            'name'       : 'eventMoteCreate',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                    ['moteId',             INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 14,
            'name'       : 'eventMoteDelete',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                    ['moteId',             INT,      2,   None],
                ],
            },
        },
    ]
    
    subCommandsNotification = [
        {
            'id'         : 1,
            'name'       : 'notifEvent',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['eventId',            INT,      4,   None],
                    [SUBID2,               INT,      1,   None],
                ],
            },
            'subCommands': subCommandsEvents,
        },
        {
            'id'         : 2,
            'name'       : 'notifLog',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['mac',                HEXDATA,  8,   None],
                    ['bytes',              HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 4,
            'name'       : 'notifData',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['utcSecs',            INT,      8,   None],
                    ['utcUsecs',           INT,      4,   None],
                    ['mac',                HEXDATA,  8,   None],
                    ['srcPort',            INT,      2,   None],
                    ['destPort',           INT,      2,   None],
                    ['data',               HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 5,
            'name'       : 'notifIpdata',
            'description': '',
            'response'   : {
                'System':  [
                    ['utcSecs',            INT,      8,   None],
                    ['utcUsecs',           INT,      4,   None],
                    ['mac',                HEXDATA,  8,   None],
                    ['data',               HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 6,
            'name'       : 'notifHr',
            'description': '',
            'response'   : {
                'System':  [
                    ['mac',                HEXDATA,  8,   None],
                    ['bytes',              HEXDATA,  None,None],
                ],
            },
        },
    ]
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    notifications = [
        {
            'id'         : 3,
            'name'       : 'manager_hello',
            'description': 'Sent by the manager to a initiate new session with a client.',
            'response'   : {
                'FIELDS':  [
                    ['version',             INT,      1,   None],
                    ['mode',                INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 20,
            'name'       : 'notification',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    [SUBID1,                INT,      1,   None],
                ]
            },
            'subCommands': subCommandsNotification,
        },
    ]
